package pers.weiney.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.weiney.mapper.UserMapper;
import pers.weiney.pojo.User;
import pers.weiney.service.UserService;
import pers.weiney.utils.Md5Util;
import pers.weiney.utils.ThreadLocalUtil;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * @Author: @Weiney
 * @ClassName: UserServiceImpl
 * @Description: 用户服务层实现类
 * @Create: 2024-08-12
 **/
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User findByUserName(String username) {
        User user = userMapper.selectByUsername(username);
        return user;
    }

    @Override
    public void register(String username, String password) {
        // 密码加密
        String md5String = Md5Util.getMD5String(password);
        // 插入用户密码
        userMapper.addUser(username, md5String);
    }

    @Override
    public void update(User user) {
        user.setUpdateTime(LocalDateTime.now());
        userMapper.updateUser(user);
    }

    @Override
    public void updateAvatar(String avatarUrl) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updateUserAvatar(avatarUrl, id);
    }

    @Override
    public void updatePwd(String newPwd) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updateUserPwd(Md5Util.getMD5String(newPwd), id);
    }
}